package com.csu.service.serviceImpl;

import com.csu.common.CommonResponse;
import com.csu.entity.StudentRecord;
import com.csu.mapper.ParentStudentMapper;
import com.csu.mapper.RecordMapper;
import com.csu.mapper.UserMapper;
import com.csu.service.RecordService;
import com.csu.vo.StudentRecordVo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ParentStudentMapper parentStudentMapper;
    /**
     * 获取自己的奖罚列表
     * @param userId
     * @return List<StudentRecordVo>
     */
    @Override
    public List<StudentRecordVo> getRecords(int userId) {
        Integer roleId = userMapper.getRoleIdByUserId(userId);
        if (roleId == null)
            return null;
        if (roleId == 3){
            //学生
            List<StudentRecord> studentRecords = recordMapper.getRecordsByUserId(userId);
            return StudentRecordToStudentRecordVo(studentRecords);
        }else {
            //家长
            List<Integer> childIds  = parentStudentMapper.getChildrenIdList(userId);
            List<StudentRecord> studentRecords = new ArrayList<>();
            for (Integer childId : childIds){
                List<StudentRecord> studentRecords1 = recordMapper.getRecordsByUserId(childId);
                studentRecords.addAll(studentRecords1);
            }
            return StudentRecordToStudentRecordVo(studentRecords);
        }

    }

    /**
     * 提交奖罚列表
     * @param studentRecordVo
     * @return int
     */

    @Override
    public int commitRecordByStudentRecord(StudentRecordVo studentRecordVo) {
        StudentRecord studentRecord = StudentRecordVoToStudentRecord(studentRecordVo);

        // 设置时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        studentRecord.setRecordDate(Timestamp.valueOf(currentDateTime));

        int count = recordMapper.insertRecord(studentRecord);
        return count;
    }

    /**
     * 获取老师发布的所有奖罚记录
     * @param teacherId
     * @return List<StudentRecordVo>
     */
    @Override
    public List<StudentRecordVo> getRecordsByTeacherId(int teacherId) {
        List<StudentRecord> studentRecords = recordMapper.getRecordsByTeacherId(teacherId);
        return StudentRecordToStudentRecordVo(studentRecords);
    }

    /**
     * 删除老师发布的奖罚记录
     * @param teacherId
     * @param recordId
     * @return int
     */
    @Override
    public int deleteRecords(int teacherId, int recordId) {
        return recordMapper.deleteRecordByTeacherIdAndRecordId(teacherId, recordId);
    }

    /**
     * 发布老师评语
     * @param studentRecord
     * @return int
     */
    @Override
    public int commitRecordOfComment(StudentRecord studentRecord) {
        // 设置时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        studentRecord.setRecordDate(Timestamp.valueOf(currentDateTime));
        return recordMapper.insertRecord(studentRecord);
    }

    /**
     * 获取学生个人的所有评语
     * @param userId
     * @return List<StudentRecordVo>
     */
    @Override
    public List<StudentRecordVo> getAllComment(int userId) {
        Integer roleId = userMapper.getRoleIdByUserId(userId);
        System.out.println("roleId:"+roleId);
        if (roleId == null)
            return null;
        if (roleId == 3){
            //学生
            List<StudentRecord> studentRecords = recordMapper.getAllComments(userId);
            return StudentRecordToStudentRecordVo(studentRecords);
        }
        else if (roleId == 1){
            //老师
            List<StudentRecord> studentRecords = recordMapper.getAllCommentsByTeacherId(userId);
            return StudentRecordToStudentRecordVo(studentRecords);
        }else {
            //家长
            List<Integer> ChildIds = parentStudentMapper.getChildrenIdList(userId);
            List<StudentRecord> studentRecords = new ArrayList<>();
            if (ChildIds.isEmpty())
                return null;
            else{
                for (Integer childId : ChildIds){
                    List<StudentRecord> studentRecords1 = recordMapper.getLastCommentOfStudent(childId);
                    studentRecords.addAll(studentRecords1);
                    System.out.println("studentRecords1"+studentRecords1);
                }
                return StudentRecordToStudentRecordVo(studentRecords);
            }
        }

    }

    /**
     * 获取最新评语
     * @param userId
     * @return StudentRecordVo
     */
    @Override
    public StudentRecordVo getLastComment(int userId) {
        Integer roleId = userMapper.getRoleIdByUserId(userId);
        System.out.println("roleId"+roleId);
        if (roleId == null)
            return null;
        if (roleId == 3){
            //学生
            List<StudentRecord> studentRecords = recordMapper.getLastCommentOfStudent(userId);
            if (studentRecords.isEmpty())
                return null;
            return StudentRecordToStudentRecordVo(studentRecords).get(0);
        }else if (roleId == 1){
            //老师
            List<StudentRecord> studentRecords = recordMapper.getLastCommentOfTeacher(userId);
            if (studentRecords.isEmpty())
                return null;
            return StudentRecordToStudentRecordVo(studentRecords).get(0);
        }else {
            //家长
            List<Integer> childIds = parentStudentMapper.getChildrenIdList(userId);
            if (childIds.isEmpty())
                return null;
            else {
                List<StudentRecord> studentRecords = new ArrayList<>();
                for (Integer childId : childIds){
                    List<StudentRecord> studentRecords1 = recordMapper.getLastCommentOfStudent(childId);
                    studentRecords.addAll(studentRecords1);
                }
                if (studentRecords.isEmpty())
                    return null;
                else
                    return StudentRecordToStudentRecordVo(studentRecords).get(0);
            }
        }
    }

    /**
     * 删除评语
     * @param recordId
     * @return int
     */
    @Override
    public int deleteComment(int recordId) {
        return recordMapper.deleteComment(recordId);
    }

    /**
     *提交赏罚信息
     */
    @Override
    public CommonResponse<Object> commitRecord(int recordType, int teacherId, int studentId, String information, String title) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        int rows = recordMapper.insertRecordNew(recordType, teacherId, studentId, information, now, title);
        if(rows != 0)
            return CommonResponse.createForSuccess("SUCCESS");
        return CommonResponse.createForError("ERROR");
    }

    /**
     * 把StudentRecord转化为StudentRecordVo
     */
    public List<StudentRecordVo> StudentRecordToStudentRecordVo(List<StudentRecord> studentRecords){
        List<StudentRecordVo> studentRecordVos = new ArrayList<>();
        for (StudentRecord studentRecord : studentRecords){
            StudentRecordVo studentRecordVo = new StudentRecordVo();
            studentRecordVo.setRecordType(studentRecord.getRecordType());
            String studentDate = studentRecord.getRecordDate().toString();
            String result = studentDate.substring(0, studentDate.lastIndexOf(":"));
            studentRecordVo.setRecordDate(result);
            studentRecordVo.setInformation(studentRecord.getInformation());
            studentRecordVo.setStudentName(userMapper.getUsernameById(studentRecord.getStudentId()));
            studentRecordVo.setTeacherName(userMapper.getUsernameById(studentRecord.getTeacherId()));
            studentRecordVo.setRecordId(studentRecord.getRecordId());
            studentRecordVo.setRecordTitle(studentRecord.getRecordTitle());

            studentRecordVos.add(studentRecordVo);
        }
        return studentRecordVos;
    }

    /**
     * StudentRecordVo转化为StudentRecord
     */
    public StudentRecord StudentRecordVoToStudentRecord(StudentRecordVo studentRecordVo){
        StudentRecord studentRecord = new StudentRecord();
        studentRecord.setStudentId(userMapper.getIdByUsername(studentRecordVo.getStudentName()));
        studentRecord.setTeacherId(userMapper.getIdByUsername(studentRecordVo.getTeacherName()));
        studentRecord.setRecordType(studentRecordVo.getRecordType());
        studentRecord.setInformation(studentRecordVo.getInformation());
        studentRecord.setRecordTitle(studentRecordVo.getRecordTitle());

        return studentRecord;
    }
}
